package com.snatch.snatchmgr.service.impl;

import org.springframework.stereotype.Service;

import com.snatch.common.constant.SnatchConstant;
import com.snatch.common.utils.DateUtil;
import com.snatch.common.vo.LiveMatch;
import com.snatch.common.vo.Matchinfo;
import com.snatch.common.vo.Playinfo;
import com.snatch.common.vo.RatioDataInput;
import com.snatch.common.vo.Result;
import com.snatch.common.vo.Sportinfo;
import com.snatch.common.vo.TradingItem;
import com.snatch.snatchmgr.entity.db.ThirdLive;
import com.snatch.snatchmgr.entity.db.ThirdMatch;
import com.snatch.snatchmgr.entity.db.ThirdPlayinfo;
import com.snatch.snatchmgr.entity.db.ThirdTradingItem;

/**
 * 数据对象转换类
 */
@Service
public class BussinessDataConvert {
	

	/**
	 * 把接收的赛事数据转换成第三方对象
	 */
	public ThirdMatch getThirdMatch(RatioDataInput ratioDataInput,Matchinfo match,final Sportinfo sportinfo) {
		ThirdMatch thirdMatch = new ThirdMatch();
		thirdMatch.setLeagueName(sportinfo.getName());
		thirdMatch.setLeagueSelfid(sportinfo.getSelfID());
		thirdMatch.setMatchSelfid(match.getMatchselfID());
		thirdMatch.setMatchName(match.getMatchName());
		thirdMatch.setTarget(ratioDataInput.getWebCode());
		thirdMatch.setGameCode(ratioDataInput.getGameCode());
		thirdMatch.setStartTime(DateUtil.parseDate(match.getStartTime(), SnatchConstant.MATCH_START_TIME));
		thirdMatch.setStartTimestr(match.getStartTime());
		thirdMatch.setMatchType(match.getMatchType());
		thirdMatch.setValid(true);
		thirdMatch.setSystemUpdateTime(System.currentTimeMillis());// 更新第三方赛事最后更新时间
		return thirdMatch;
	}

	
	/**
	 * 根据玩法生成key
	 * 让球key:时间范围(0,1)+allow+主(指标)或：时间范围(0,1)+allow+主-(指标)
	 * 大小球key:时间范围(0,1)+bigsmall+指标
	 * 其他玩法key:时间范围(0,1)+玩法名称
	 * @param playinfo
	 * @return
	 */
	public String getPlayinfoKey(Playinfo playinfo){
		String key=playinfo.getName();
		if(key.equals("allow") && playinfo.getTradingItems().size()>0){
			TradingItem t= playinfo.getTradingItems().get(0);
			for(TradingItem ti : playinfo.getTradingItems()){
				if(ti.getName().indexOf("主")!=-1){
					t=ti;
					break;
				}
			}
			return playinfo.getGametime()+key+t.getName();
		}else if(key.equals("bigsmall") && playinfo.getTradingItems().size()>0 ){
			return playinfo.getGametime()+key+playinfo.getTradingItems().get(0).getName().substring(1);
		}else{
			return playinfo.getGametime()+key;
		}
	}	
	
	/**
	 * 把接收的玩法数据转换成第三方对象
	 */
	public ThirdPlayinfo getThirdPlayinfo(Playinfo playinfo) {
		ThirdPlayinfo thirdPlayinfo = new ThirdPlayinfo();
		thirdPlayinfo.setName(playinfo.getName());
		thirdPlayinfo.setHandicap(playinfo.getHandicap());
		thirdPlayinfo.setSelfID(playinfo.getSelfID());
		thirdPlayinfo.setGametime(playinfo.getGametime());
		return thirdPlayinfo;
	}

	
	/**
	 * 把接收的交易项数据转换成第三方对象
	 */
	public ThirdTradingItem getThirdTradingItem(TradingItem tradingItem, String thirdSystem) {
		ThirdTradingItem thirdTradingItem = new ThirdTradingItem();
		thirdTradingItem.setName(tradingItem.getName());
		thirdTradingItem.setSelfID(tradingItem.getSelfID());
		thirdTradingItem.setRatio(tradingItem.getRatio());
		thirdTradingItem.setTarget(thirdSystem);
		return thirdTradingItem;
	}
	
	
	public ThirdLive getLiveData(RatioDataInput ratioDataInput,LiveMatch liveMatch){
		ThirdLive thirdLive = new ThirdLive();
		thirdLive.setOnlyCode(ratioDataInput.getOnlyCode());
		thirdLive.setTarget(ratioDataInput.getWebCode());
		thirdLive.setGameCode(ratioDataInput.getGameCode());
		thirdLive.setLeagueName(liveMatch.getLeagueName());
		thirdLive.setMatchName(liveMatch.getMatchName());
		thirdLive.setLeagueSelfid(liveMatch.getLeagueSelfID());
		thirdLive.setMatchSelfid(liveMatch.getMatchSelfID());
		thirdLive.setShowTime(liveMatch.getShowTime());
		thirdLive.setStatus(liveMatch.getStatus());
		thirdLive.setCloseStatus(liveMatch.getCloseStatus());
		thirdLive.setHomeGoal(liveMatch.getHomeGoal());
		thirdLive.setAwayGoal(liveMatch.getAwayGoal());
		thirdLive.setHomeYc(liveMatch.getHomeYC());
		thirdLive.setAwayYc(liveMatch.getAwayYC());
		return thirdLive;
	}
	
	
	public ThirdLive getThirdLive(RatioDataInput ratioDataInput,Result result){
		ThirdLive thirdLive = new ThirdLive();
		thirdLive.setOnlyCode(result.getOnlyCode());
		thirdLive.setStatus(result.getStatus());
		thirdLive.setTarget(result.getTarget());
		thirdLive.setLeagueSelfid(result.getLeagueSelfid());
		thirdLive.setMatchSelfid(result.getMatchSelfid());
		thirdLive.setGameCode(result.getGameCode());
		thirdLive.setMatchError(result.getMatchError());
		thirdLive.setLeagueName(result.getLeagueName());
		thirdLive.setMatchName(result.getMatchName());
		thirdLive.setStartTime(result.getStartTime());
		thirdLive.setHomeGoal(result.getHomeGoal());
		thirdLive.setHhtGoal(result.getHhtGoal());
		thirdLive.setShrhGoal(result.getShrhGoal());
		thirdLive.setOnehGoal(result.getOnehGoal());
		thirdLive.setTwohGoal(result.getTwohGoal());
		thirdLive.setThreehGoal(result.getThreehGoal());
		thirdLive.setFourhGoal(result.getFourhGoal());
		thirdLive.setFivehGoal(result.getFivehGoal());
		thirdLive.setSixhGoal(result.getSixhGoal());
		thirdLive.setSevenhGoal(result.getSevenhGoal());
		thirdLive.setEthGoal(result.getEthGoal());
		thirdLive.setScorehGoal(result.getScorehGoal());
		thirdLive.setAwayGoal(result.getAwayGoal());
		thirdLive.setHatGoal(result.getHatGoal());
		thirdLive.setShraGoal(result.getShraGoal());
		thirdLive.setOneaGoal(result.getOneaGoal());
		thirdLive.setTwoaGoal(result.getTwoaGoal());
		thirdLive.setThreeaGoal(result.getThreeaGoal());
		thirdLive.setFouraGoal(result.getFouraGoal());
		thirdLive.setFiveaGoal(result.getFiveaGoal());
		thirdLive.setSixaGoal(result.getSixaGoal());
		thirdLive.setSevenaGoal(result.getSevenaGoal());
		thirdLive.setEtaGoal(result.getEtaGoal());
		thirdLive.setScoreaGoal(result.getScoreaGoal());
		return thirdLive;
	}
	
	
}
